package service.version4;


import dao.version4.UserInfoDao;
import dao.version4.UserInfoHistoryDao;

import utils.version4.TransactionManager;

public class UserService {
    TransactionManager transactionManager;

    public UserService(TransactionManager transactionManager) {

        this.transactionManager = transactionManager;
    }

    public void archive(String userid, String username) {

        try {
            transactionManager.start();

            UserInfoDao needToArchive = new UserInfoDao(transactionManager.getFactory());
            UserInfoHistoryDao archivingUser = new UserInfoHistoryDao(transactionManager.getFactory());

            needToArchive.deleteById(userid);
            archivingUser.insert(userid, username);

            transactionManager.commit();
        } catch (RuntimeException e) {
            transactionManager.rollback();
            System.out.println(e.getMessage());
            throw e;
        } finally {
            transactionManager.getFactory().close();
        }


    }
}
